﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using NUnit.Framework;
using Xtee.Core.Adapter;

namespace Xtee.Core.Test
{
    /// <summary>
    /// 
    /// </summary>
    [SetUpFixture]
    public class GlobalSetup
    {
        /// <summary>
        /// 
        /// </summary>
        [SetUp]
        public void Start()
        {
            log4net.Config.XmlConfigurator.Configure();
           
            if (!new List<Process>(Process.GetProcesses()).Exists(x => x.ProcessName.StartsWith("WebDev.WebServer", StringComparison.OrdinalIgnoreCase)))
            {
                var devserverRoot = @"C:\Program Files (x86)\Common Files\microsoft shared\DevServer";

                var webServerExePath = Path.Combine(devserverRoot, @"11.0\WebDev.WebServer40.EXE");
                if (!File.Exists(webServerExePath))
                {
                    webServerExePath = Path.Combine(devserverRoot, @"10.0\WebDev.WebServer40.EXE");
                }
                if (!File.Exists(webServerExePath))
                {
                    webServerExePath = Environment.GetEnvironmentVariable("SYSTEMROOT") + @"\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe";
                }
                if (!File.Exists(webServerExePath))
                {
                    webServerExePath = Path.Combine(devserverRoot, @"9.0\WebDev.WebServer.EXE");
                }

                foreach (var args in ServerStartArgs)
                {
                    var process = Process.Start(webServerExePath, args);
                    process.WaitForInputIdle();

                } 
            }

            Console.WriteLine("Pinging sites.");
            var client = new WebClient();
            foreach (var site in Sites)
            {
                Console.WriteLine("Pinging : "+site);
                var dataa = client.DownloadString(site);
                Assert.That(dataa, Is.EqualTo(CurrentAdapterVersion.Text));
            }
            Console.WriteLine("Done pinging sites.");
           
        }

        private IEnumerable<string> Sites
        {
          get

          {
                yield return "http://localhost:6005/adapter.asmx";
                yield return "http://localhost:6006/adapter.asmx";
                yield return "http://localhost:6007/AdapterXtee.asmx";
                yield return "http://localhost:6008/AdapterXtee.asmx";
          }
        } 
        private IEnumerable<string> ServerStartArgs
        {
            get
            {
                yield return String.Format("/port:{0} /path:\"{1}\"", "6005", Path.GetFullPath(@"..\..\..\..\Examples\Calculator\Server"));
                yield return String.Format("/port:{0} /path:\"{1}\"", "6006", Path.GetFullPath(@"..\..\..\..\Examples\PaintShop\Server"));
                yield return String.Format("/port:{0} /path:\"{1}\"", "6007", Path.GetFullPath(@"..\..\..\..\Examples\DocumentRepositoryExample\Service"));
                yield return String.Format("/port:{0} /path:\"{1}\"", "6008", Path.GetFullPath(@"..\..\..\..\Examples\DocumentRepositoryExample\Server_HandlesAttachmentInMemory"));
            }
        }  
    }
}
